package yxy.game.pm2.command.achievement;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TaskMessage;
import yxy.game.pm2.module.achievement.AchievementHandle;
import yxy.game.pm2.module.achievement.AchievementModule;

import java.util.Map;

/** 任务成就 成就奖励领取 */
@MessageType(proto = Command.AchievementBonusReceive, classtype = TaskMessage.AchievementBonusReceiveRequest.class)
public class CMD_AchievementBonusReceive extends Command<TaskMessage.AchievementBonusReceiveRequest> {

	@Override
	public void execute(MessageSource messageSource, TaskMessage.AchievementBonusReceiveRequest request) {
		String playerCode = messageSource.getTag();
		String code = request.getCode();

		TaskMessage.AchievementBonusReceiveResponse.Builder response = TaskMessage.AchievementBonusReceiveResponse.newBuilder();

		Map<String, AchievementModule> all = AchievementModule.all(playerCode);
		for (AchievementModule module : all.values()) {
			if (module.getTaskCodes().size() <= 0) {
				continue;
			}
			String taskCode = module.getTaskCodes().get(0);
			if (code.equals(taskCode)) {
				Result result = new AchievementHandle().reward(playerCode, code, module, response);
				send(messageSource, response, result);
				return;
			}
		}
	}

	private void send(MessageSource messageSource, TaskMessage.AchievementBonusReceiveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}